using TMPro;
using UnityEditor.PackageManager;
using UnityEngine;
using UnityEngine.UI;

public class RegisterPanel : BasePanel
{
    public Button buttonSure;
    public Button buttonCancer;

    public TMP_InputField inputUN;
    public TMP_InputField inputPS;

    public override void Init()
    {
        //确认
        buttonSure.onClick.AddListener(() =>
        {
            //判断是否合理
            if (inputUN.text.Length <= 6 || inputPS.text.Length <= 6)
            {
                //提示不合法
                TipPanel panel = UIManager.Instance.ShowPanel<TipPanel>();
                panel.ChangeTipsText("账号/密码必须大于6位");
                return;
            }
            //调用注册逻辑，成功返回true
            if (LoginManager.Instance.RegisterUser(inputUN.text, inputPS.text))
            {
                //注册成功清理登录数据（重置）
                LoginManager.Instance.CleanLoginData();
                //显示登录面板
                LoginPanel loginPanel = UIManager.Instance.ShowPanel<LoginPanel>();
                loginPanel.SetInfo(inputUN.text, inputPS.text);

                //隐藏注册面板
                UIManager.Instance.HidePanel<RegisterPanel>();
                //显示提示面板
                TipPanel panel = UIManager.Instance.ShowPanel<TipPanel>();
                panel.ChangeTipsText("注册成功");
            }
            else
            {
                //注册失败//
                //显示提示面板
                TipPanel panel = UIManager.Instance.ShowPanel<TipPanel>();
                panel.ChangeTipsText("注册失败,用户名已存在");
                //清零
                inputUN.text = "";
                inputPS.text = "";
            }

        });
        //取消
        buttonCancer.onClick.AddListener(() =>
        {
            //隐藏注册面板
            UIManager.Instance.HidePanel<RegisterPanel>();
            //显示登录面板
            UIManager.Instance.ShowPanel<LoginPanel>();
        });

    }


}
